//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lalifa.utils;

import android.annotation.TargetApi;
import android.util.ArrayMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

public final class LiveDataBus {
    private final Map<String, BusMutableLiveData<Object>> bus;

    @TargetApi(19)
    private LiveDataBus() {
        this.bus = new ArrayMap();
    }

    public static LiveDataBus get() {
        return SingletonHolder.DEFAULT_BUS;
    }

    public <T> MutableLiveData<T> with(String key, Class<T> type) {
        if (!this.bus.containsKey(key)) {
            this.bus.put(key, new BusMutableLiveData());
        }

        return (MutableLiveData)this.bus.get(key);
    }

    public MutableLiveData<Object> with(String key) {
        return this.with(key, Object.class);
    }

    private static class BusMutableLiveData<T> extends MutableLiveData<T> {
        private Map<Observer, Observer> observerMap;

        private BusMutableLiveData() {
            this.observerMap = new ArrayMap();
        }

        public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
            super.observe(owner, observer);

            try {
                this.hook((Observer<T>) observer);
            } catch (Exception var4) {
                var4.printStackTrace();
            }

        }

        public void observeForever(@NonNull Observer<? super T> observer) {
            if (!this.observerMap.containsKey(observer)) {
                this.observerMap.put(observer, new ObserverWrapper(observer));
            }

            super.observeForever((Observer)this.observerMap.get(observer));
        }

        public void removeObserver(@NonNull Observer<? super T> observer) {
            Observer realObserver = null;
            if (this.observerMap.containsKey(observer)) {
                realObserver = (Observer)this.observerMap.remove(observer);
            } else {
                realObserver = observer;
            }

            super.removeObserver(realObserver);
        }

        private void hook(@NonNull Observer<T> observer) throws Exception {
            Class<LiveData> classLiveData = LiveData.class;
            Field fieldObservers = classLiveData.getDeclaredField("mObservers");
            fieldObservers.setAccessible(true);
            Object objectObservers = fieldObservers.get(this);
            Class<?> classObservers = objectObservers.getClass();
            Method methodGet = classObservers.getDeclaredMethod("get", Object.class);
            methodGet.setAccessible(true);
            Object objectWrapperEntry = methodGet.invoke(objectObservers, observer);
            Object objectWrapper = null;
            if (objectWrapperEntry instanceof Map.Entry) {
                objectWrapper = ((Map.Entry)objectWrapperEntry).getValue();
            }

            if (objectWrapper == null) {
                throw new NullPointerException("Wrapper can not be bull!");
            } else {
                Class<?> classObserverWrapper = objectWrapper.getClass().getSuperclass();
                Field fieldLastVersion = classObserverWrapper.getDeclaredField("mLastVersion");
                fieldLastVersion.setAccessible(true);
                Field fieldVersion = classLiveData.getDeclaredField("mVersion");
                fieldVersion.setAccessible(true);
                Object objectVersion = fieldVersion.get(this);
                fieldLastVersion.set(objectWrapper, objectVersion);
            }
        }
    }

    private static class ObserverWrapper<T> implements Observer<T> {
        private Observer<T> observer;

        public ObserverWrapper(Observer<T> observer) {
            this.observer = observer;
        }

        public void onChanged(@Nullable T t) {
            if (this.observer != null) {
                if (this.isCallOnObserve()) {
                    return;
                }

                this.observer.onChanged(t);
            }

        }

        private boolean isCallOnObserve() {
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            if (stackTrace != null && stackTrace.length > 0) {
                StackTraceElement[] var2 = stackTrace;
                int var3 = stackTrace.length;

                for(int var4 = 0; var4 < var3; ++var4) {
                    StackTraceElement element = var2[var4];
                    if ("android.arch.lifecycle.LiveData".equals(element.getClassName()) && "observeForever".equals(element.getMethodName())) {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    private static class SingletonHolder {
        private static final LiveDataBus DEFAULT_BUS = new LiveDataBus();

        private SingletonHolder() {
        }
    }
}
